unit Kassa;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Mask, ToolEdit, CurrEdit;

type
  TfmKassa = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    btnOk: TButton;
    edtPrihod: TCurrencyEdit;
    edtSaldo: TCurrencyEdit;
    edtRashod: TCurrencyEdit;
    edtInkass: TCurrencyEdit;
    edtAvans: TCurrencyEdit;
    procedure FormShow(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    procedure DisplayData;
    function GetAvans(Pri: integer): currency;
    function GetSaldo: currency;
    function GetTurnover(Income: boolean): currency;
  public
    { Public declarations }
  end;

const
  FoxBools: array[boolean] of string = ('.F.', '.T.');

var
  fmKassa: TfmKassa;

implementation
uses DM, AdmBin;
{$R *.DFM}

function TfmKassa.GetAvans(Pri: integer): currency;
begin
  with dmData.qKassa do begin
    Close;
    SelectSQL.Text := 'SELECT SUM(a."SUM") FROM AVANS a ' +
      'WHERE DAT = :dat AND PRI = :pri';
    ParamByName('dat').AsDate := OperDay;
    ParamByName('pri').AsInteger := Pri;
    Open;
    result := Fields[0].AsCurrency;
    Close;
  end;
end;

function TfmKassa.GetTurnover(Income: boolean): currency;
begin
  if not Income then begin
    result := 0;
  end
  else
    with dmData.qKassa do begin
      Close;
      SelectSQL.Text := 'select * from get_turnover( :p_date, :p_punkt )';
      ParamByName('p_date').AsDate := OperDay;
      ParamByName('p_punkt').AsInteger := PunktNum;
      Open;
      result := Fields[0].AsCurrency;
      Close;
    end;
end;

function TfmKassa.GetSaldo: currency;
begin
  result := edtAvans.Value - edtInkass.Value + edtPrihod.Value
            - edtRashod.Value;
end;

procedure TfmKassa.DisplayData;
begin
  edtAvans.Value := GetAvans(1);
  edtInkass.Value := GetAvans(2);
  edtPrihod.Value := GetTurnover(True);
  edtRashod.Value := GetTurnover(False);
  edtSaldo.Value := GetSaldo;
end;

procedure TfmKassa.FormShow(Sender: TObject);
begin
  DisplayData;
end;

procedure TfmKassa.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_ESCAPE then Close;
end;

end.
